package com.it.exprezoorecords.adapter;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TrackAdapter extends ArrayAdapter<Track> {

	private int				resource;
	private LayoutInflater	inflater;
	final String TAG = "TrackAdapter";
	
	public TrackAdapter(Context context, int resourceId, List<Track> objects) {
		super( context, resourceId, objects );
		resource = resourceId;
		inflater = LayoutInflater.from( context );
		
	}
	
	
	@Override
	public View getView ( int position, View convertView, ViewGroup parent )
	{
		// Recuperiamo l'oggetti che dobbiamo inserire a questa posizione
		final Track track = getItem( position );
		TrackViewCache viewCache;
		
		if( convertView == null )
		{
			convertView = ( RelativeLayout ) inflater.inflate( resource, null );
			viewCache = new TrackViewCache( convertView );
			convertView.setTag( viewCache );
		}
		else
		{
			convertView = ( RelativeLayout ) convertView;
			viewCache = ( TrackViewCache ) convertView.getTag();
		}

		// Prendiamo le view dalla cache e mettiamoci i valori

		TextView tvAuthor = viewCache.getTvAuthor();
		tvAuthor.setText(track.getAuthor());
		
		TextView tvTitle = viewCache.getTvTitle();
		tvTitle.setText(track.getTitle());
	
		return convertView;
	}

}
